package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	import mx.core.FlexBitmap;
	
	public class shapePlay1 extends Sprite
	{
		public function shapePlay1()
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage, false, 0, true);
			trace('super');
		}
		private function handleAddedToStage(event:Event):void
		{
			trace('handleAddedToStage');
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			addEventListener(Event.ENTER_FRAME, handleEnterFrame,	false, 0, true);
			addChild(bmp);
		}
		
		private var brush:Sprite;
		private const bmpd:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false,0x000000);
		private const bmp:Bitmap = new Bitmap(bmpd,PixelSnapping.NEVER,false);
		private var numDraws:uint=100;
		
		private const timer:Timer = new Timer(5000,1);
		private function handleEnterFrame(event:Event):void
		{
			/*if (timer.running)
				return;*/
			const stage:Stage = this.stage;
		/*	for (var i:int = 1 << 12; i -- > 0;)
			{
				graphics.lineStyle(1,uint());
				graphics.drawCircle(Math.random()* stage.stageWidth, Math.random() * stage.stageHeight, .2);
			}
			
			
	*/		step++;
			bmpd.lock();
			for(var i:int=0;i<numDraws;i++)
			{
				var clr:uint=Math.random()*0xFFFFFF;
				var loc:Point=new Point(Math.round(Math.random()* stage.stageWidth), Math.round(Math.random() * stage.stageHeight));
				Math.random()>.5 ? bmpd.setPixel(loc.x,loc.y,clr) : bmpd.fillRect(new Rectangle(loc.x,loc.y,10,10),clr);
			}
			
			if(Math.random()>.6)
			{
				doBlur();
			}
			
			//if(step%10==0)
			//{
				enlarge()
			//}
			bmpd.unlock();
			timer.start();
		}
		
		private function enlarge():void
		{
			var m:Matrix=new Matrix();
			//m.translate(-bmpd.width*.05,-bmpd.height*.05);
			m.translate(-mouseX,-mouseY);
			m.scale(1.1,1.1);
			m.rotate(.01);
			var ct:ColorTransform = new ColorTransform();
			ct.alphaMultiplier=.5;
			
			bmpd.draw(bmpd,m,ct);
		}
		private var step:uint=0;
		private function doBlur():void
		{
			bmpd.applyFilter(bmpd,bmpd.rect,new Point(),blurF);
		}
		private const blurF:BlurFilter = new BlurFilter(1.5,1.5,1);
	}
}